//全局变量
var seq =0;
var butonva=0;
var thispage=1;
var pages;


//局部变量
showdate("notype",'name','tel','School','sex',thispage);

// ajks
    function showdate(type,name,tel,School,sex,page) {
        $.ajax({
            url: "IndexRole",
            type: "post",
            dataType: "JSON",
            data: {
                one:type,
                two:name,
                three:tel,
                four:School,
                six:sex,
                page:page,
            },
            success: function(data) {
                console.log(data);
                show(data);

    
            },
            error: function(err) {
                console.log(err)
            },
        });
    
    }
//修改 ajks
function showdate1(type,name,Phone,School,sex,email,seq,job) {
    $.ajax({
        url: "IndexRole2",
        type: "post",
        dataType: "JSON",
        data: {
            one:type,
            two:name,
            three:Phone,
            four:School,
            five:sex,
            six:email,
            seven:seq,
            eight:job,
        },
        success: function(data) {
            usershow(data);
            console.log(data);

        },
        error: function(err) {
            console.log(err)
        },
    });

}

function show(data){
    //清除表格中数据
    document.getElementById("tbody").innerHTML="";
    pages=data.pages;
    for(var i=0;i<data.list.length;i++){

        createTable(i,data.list[i].uSER_NAME,data.list[i].tEL,data.list[i].sEX,data.list[i].sCHOOL,data.list[i].jOB,data.list[i].eMAIL,data.list[i].sTATE_NAME,data.list[i].uSER_ID);
    }
}

function createTable(i,username,phone,sex,school,job,email,state,user_seq){
    // seq =user_seq;
    var tbody =document.getElementById("tbody");

    var tr =document.createElement("tr");

    var td =document.createElement("td");
    td.innerHTML=user_seq;
    td.id="td"+user_seq;

    var td1 =document.createElement("td");
    td1.innerHTML=username;

    var td2 =document.createElement("td");
    td2.innerHTML=phone;

    var td3 =document.createElement("td");
    td3.innerHTML=sex;

    var td4 =document.createElement("td");
    td4.innerHTML=school;

    var td5 =document.createElement("td");
    td5.innerHTML=job;
    td5.id="job"+i;

    var td6 =document.createElement("td");
    td6.innerHTML=email;
    td6.id ="email"+i;

    var td7 =document.createElement("td");
    td7.innerHTML=state;

    var td8 = document.createElement("td");

    var bj =document.createElement("input");
    bj.type='button';
    bj.value='编辑';
    bj.onclick =function(){
        seq =document.getElementById(td.id).innerText;
        var name =document.getElementById("Upname").value=username;
        var Phone =document.getElementById("Uptel").value=phone;
        var School =document.getElementById("UpSchool").value=school;
        var job =document.getElementById(td5.id).innerHTML;
        var email =document.getElementById(td6.id).innerHTML;
        var job =document.getElementById("UpJob").value=job;  
         var email =document.getElementById("Upemail").value=email;
        // 获取下拉框的值
        // var kd1 = document.getElementById('sex').value=sex;
        var a=document.getElementById("bj");
        a.style.display ='block';
    }
    var jy =document.createElement("input");
    jy.type='button';
    jy.id="but"+user_seq;
    if(state=='启用'){
            jy.value='禁用';
            butonva="2";
    }else {
        jy.value='启用';
        butonva='1';
    }

    jy.onclick =function (){
        seq =document.getElementById(td.id).innerText;
       if(document.getElementById( jy.id).value==='禁用') {
           butonva="2";
       }else{
           butonva='1';
       }
        showdate1("Upstate",butonva,seq,'School','sex','email','seq','job');
    }

    var del =document.createElement("input");
    del.type='button';
    del.value='删除';
    del.onclick =function(){
        seq =document.getElementById(td.id).innerText;
        showdate1("delete",seq,"seq",'School','sex','email','seq','job');
    }

    tr.appendChild(td);
    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);
    tr.appendChild(td4);
    tr.appendChild(td5);
    tr.appendChild(td6);
    tr.appendChild(td7);
    tr.appendChild(td8);
    td8.appendChild(bj);
    td8.appendChild(jy);
    td8.appendChild(del);
    tbody.appendChild(tr);
}


// showdate1(type,name,Phone,School,sex,email,seq) 编辑
function update(){
    var name =document.getElementById("Upname").value;
    var Phone =document.getElementById("Uptel").value;
    var School =document.getElementById("UpSchool").value;
    var job =document.getElementById("UpJob").value;
    // 获取下拉框的值
    var kd1 = document.getElementById('sex');
	var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;
    
    var email =document.getElementById("Upemail").value;
    alert("bj"+name+Phone+School+idnex1+email+"nini"+seq);
    showdate1("bj",name,Phone,School,idnex1,email,seq,job);
    var a=document.getElementById("bj");
    a.style.display ='none';
}

function Select(){ 
    var name =document.getElementById("username").value;
    var tel =document.getElementById("userphone").value;
    // 获取下拉框的值
    var kd1 = document.getElementById('Schooli');
	var d1 = kd1.selectedIndex;
    var idnex1 = kd1.options[d1].value;

    //获取radio中的值
   var sex= $("input[name='radioOptionsExample']:checked").val();
   showdate("Select",name,tel,idnex1,sex,thispage);
  }  

  function  usershow(){
      showdate("notype",'name','tel','School','sex',thispage);
  }
  function  cancle(){
        document.getElementById("bj").style.display="none";
  }
function Arcalefron(){
    if(thispage-1>0){
        thispage=(thispage-1);
        showdate("notype",'name','tel','School','sex',thispage);
    }else {
        alert("没有上一页");
    }
}
function Arcalefronnext() {
    if (thispage < pages) {
        thispage = (thispage + 1);
        showdate("notype",'name','tel','School','sex',thispage);
    } else {
        alert("没有下一页");
    }
}